package com.xinchuang.mapper.targetSystem;

import com.xinchuang.vo.FindTargetAndSubType;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/18 11:30
 */
public interface MyUtilMapper {

    /**
     * 查询目标的类型参数
     * @param type
     * @return
     */
    @Select(value = {
            "SELECT tt.type_id,tt.type_name,ts.id,ts.key_name,ts.key_value,ts.key_type,ts.show_type,1 AS status_type",
            "FROM target_type tt",
            "JOIN target_special_attribute ts ON tt.type_id = ts.type_id",
            "WHERE tt.type_id = #{typeId}"
    })
    List<FindTargetAndSubType> findTargetType(String type);

    /**
     * 查询目标与子目标的关联的类型参数
     * @param typeId
     * @return
     */
    @Select(value = {
            "SELECT tt.type_id,tt.type_name,ts.id,ts.key_name,ts.key_value,ts.key_type,ts.show_type,1 AS status_type",
            "FROM target_type tt",
            "JOIN target_special_attribute ts ON tt.type_id = ts.type_id",
            "WHERE tt.type_id = #{typeId}",
            "UNION",
            "SELECT tt.type_id,tt.type_name,ts.id,ts.key_name,ts.key_value,ts.key_type,ts.show_type,2 AS status_type",
            "FROM target_sub_type tt",
            "JOIN target_special_attribute ts ON tt.type_id = ts.type_id",
            "WHERE tt.parent_id = #{typeId}"
    })
    List<FindTargetAndSubType> findTargetAndSubType(String typeId);

    /**
     * 查询子目标的关联的类型参数
     * @param subTypeId
     * @return
     */
    @Select(value = {
            "SELECT tt.type_id,tt.type_name,ts.id,ts.key_name,ts.key_value,ts.key_type,ts.show_type,2 AS status_type",
            "FROM target_sub_type tt",
            "JOIN target_special_attribute ts ON tt.type_id = ts.type_id",
            "WHERE tt.type_id = #{typeId}"
    })
    List<FindTargetAndSubType> findSubType(String subTypeId);

    /**
     * 查询目标下子目标类型某个属性参数的最大值
     * @param targetId
     * @param typeId
     * @param keyId
     * @return
     */
    @Select(value = {
            "SELECT IFNULL(MAX(key_value),0) FROM target_sub_msg_table WHERE sub_id IN (",
            "SELECT tr.sub_id FROM target_sub_relation_table tr",
            "JOIN target_sub_type ts ON 1 = 1",
            "WHERE target_id = #{targetId}",
            "AND ts.type_id = #{typeId}",
            ") AND key_id = #{keyId}"
    })
    Object getSubKeyIdMaxValue(String targetId, String typeId, String keyId);

    /**
     * 查询目标下某个类型的子目标的数量
     * @param targetId
     * @param typeId
     * @return
     */
    @Select(value = {
            "SELECT tr.sub_id FROM target_sub_relation_table tr",
            "JOIN target_sub_type ts ON 1 = 1",
            "WHERE target_id = #{targetId}",
            "AND ts.type_id = #{typeId}"
    })
    List<String> getSubListBySubType(String targetId, String typeId);

}
